package com.dz.Incubator.controller;

import com.dz.Incubator.config.Pagination;
import com.dz.Incubator.config.ReturnCode;
import com.dz.Incubator.entity.InvokeResult;
import com.dz.Incubator.entity.ServiceContentEntity;
import com.dz.Incubator.service.ServiceContentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/a/zx/serviceContent")
@Slf4j
public class ServiceContentController {


    @Autowired
    private ServiceContentService serviceContentService;

    /**
     * 分页查询 serviceContent 对象
     *
     */
    @RequestMapping(value = "/getPageList", method = RequestMethod.POST)
    @ResponseBody
    public InvokeResult<Pagination> getPageList(@RequestBody Pagination pagination) {
        InvokeResult invokeResult = null;
        try {
            if (null == pagination) {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
            Pagination pageInfo = serviceContentService.findByPagination(pagination);
            if (null != pageInfo && null != pageInfo.getList() && pageInfo.getList().size() > 0) {
                invokeResult = InvokeResult.success(pageInfo);
            } else {
                invokeResult = InvokeResult.pass_no_data();
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 获取单个ServiceContentEntity 对象
     *
     * @param id Id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public InvokeResult<ServiceContentEntity> get(@PathVariable Long id) {
        InvokeResult invokeResult;
        try {
            if (null != id) {
                ServiceContentEntity serviceContentEntity = serviceContentService.get(id);
                if (null == serviceContentEntity) {
                    invokeResult = InvokeResult.pass_no_data();
                }
                invokeResult = InvokeResult.success(serviceContentEntity);
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 通过服务类型查询服务项目
     *
     */
    @RequestMapping(value = "/type", method = RequestMethod.GET)
    @ResponseBody
    public InvokeResult<List<ServiceContentEntity>> getServiceInfoByType(@RequestParam Long id) {
        InvokeResult invokeResult;
        try {
            if (null != id) {
                List<ServiceContentEntity> serviceContentEntity = serviceContentService.getServiceInfoByType(id);
                if (null == serviceContentEntity) {
                    invokeResult = InvokeResult.pass_no_data();
                }
                invokeResult = InvokeResult.success(serviceContentEntity);
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 删除单个serviceContentEntity 对象
     *
     * @param id 删除数据 ID
     */
    @RequestMapping(value = "/del", method = RequestMethod.GET)
    @ResponseBody
    public InvokeResult delete(@RequestParam Long id) {
        InvokeResult invokeResult = null;
        try {
            if (null != id) {
                serviceContentService.delete(id);
                invokeResult = InvokeResult.success();
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 集合查找 serviceContentEntity 对象
     *
     */
    @RequestMapping(value = "/find", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public InvokeResult<List<ServiceContentEntity>> find(@RequestParam("ids") List<Long> ids) {
        InvokeResult invokeResult = null;
        try {
            if (null != ids && ids.size() > 0) {
                Long[] skuIds = ids.toArray(new Long[ids.size()]);
                List<ServiceContentEntity> serviceContentEntitys = serviceContentService.find(skuIds);
                if (null != serviceContentEntitys && serviceContentEntitys.size() > 0) {
                    invokeResult = InvokeResult.success(serviceContentEntitys);
                } else {
                    invokeResult = InvokeResult.pass_no_data();
                }
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 查找所有 serviceContentEntity 对象
     */
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ResponseBody
    public InvokeResult<List<ServiceContentEntity>> findAll() {
        InvokeResult invokeResult = null;
        try {
            List<ServiceContentEntity> serviceContentEntitys = serviceContentService.findAll();
            if (null != serviceContentEntitys && serviceContentEntitys.size() > 0) {
                invokeResult = InvokeResult.success(serviceContentEntitys);
            } else {
                invokeResult = InvokeResult.pass_no_data();
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 新增数据
     *
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public InvokeResult<ServiceContentEntity> add(@RequestBody ServiceContentEntity serviceContentEntity) {
        InvokeResult invokeResult = null;
        try {
            if (null != serviceContentEntity) {
                invokeResult = InvokeResult.success(serviceContentService.insert(serviceContentEntity));
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 修改数据
     *
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public InvokeResult<ServiceContentEntity> update(@RequestBody ServiceContentEntity serviceContentEntity) {
        InvokeResult invokeResult = null;
        try {
            if (null != serviceContentEntity && null != serviceContentEntity.getId()) {
                invokeResult = InvokeResult.success(serviceContentService.update(serviceContentEntity));
            } else {
                return InvokeResult.failure(ReturnCode.PARAM_ERROR.getCode(), ReturnCode.PARAM_ERROR.getMsg());
            }
        } catch (Exception e) {
            invokeResult = InvokeResult.error();
            log.error(e.getMessage(), e);
        }
        return invokeResult;
    }

    /**
     * 查询上一条和下一条数据
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "findPrevAndNext/{id}", method = RequestMethod.GET)
    @ResponseBody
    public InvokeResult<Map<String,ServiceContentEntity>> findPrevAndNext(@PathVariable int id) {
        return InvokeResult.success(serviceContentService.findPrevAndNext(id));
    }
}

